﻿using ArticleandSiteData;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// “空白页”项模板在 http://go.microsoft.com/fwlink/?LinkId=234238 上有介绍

namespace SplitViewOptions
{
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class ShareTextPage : Page
    {
        public ShareTextPage()
        {
            this.InitializeComponent();
        }
        private async void addbtn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string mode = string.Empty;
                string userid = string.Empty;
                string username = string.Empty;
                if (App.Loginuser == null)
                {

                    userid = "unknowuserid" + Guid.NewGuid();
                    username = "unkownusename" + Guid.NewGuid();
                }


                if (App.Loginuser != null)
                {
                    userid = App.Loginuser.objectId;
                    username = App.Loginuser.username;
                }
                if (string.IsNullOrEmpty(urlbox.Text))
                {
                    mode = "text";
                }
                if (!urlbox.Text.StartsWith("http") || !urlbox.Text.StartsWith("www"))
                {
                    mode = "text";
                }
                string jsonstr = string.Empty;
                jsonstr = "{" + "\"" + "url" + "\"" + ":" + "\"" + urlbox.Text + "\"" + "," + "\"" + "name" + "\"" + ":" + "\"" + namebox.Text + "\"" + "," + "\"" + "userid" + "\"" + ":" + "\"" + userid + "\"" + "," + "\"" + "username" + "\"" + ":" + "\"" + username + "\"" + "," + "\"" + "type" + "\"" + ":" + "\"" + mode + "\"" + "," + "\"" + "keyword" + "\"" + ":" + "\"" + string.Empty + "\"" + "}";
                StringContent content = new StringContent(jsonstr);
                HttpResponseMessage oneHttpResponseMessage = await BmobWebsiteData.AddOne<HttpResponseMessage>(App.ShareSiteorArticle, jsonstr);
                if (oneHttpResponseMessage.StatusCode == System.Net.HttpStatusCode.Created)
                {
                    await App.showmessage("分享成功");
                    //MessageDialog one = new MessageDialog("分享成功");
                    //await one.ShowAsync();
                }
                else
                {
                    await App.showmessage("分享失败");
                    //MessageDialog one = new MessageDialog("分享失败");
                    //await one.ShowAsync();
                }
            }
            catch (Exception ex)
            {

            }
        }

    }
}
